DAY37:Find the odd int


Posted by birdbirdmurmur on 2023-08-19

題目連結

https://www.codewars.com/kata/54da5a58ea159efa38000836

解法

function findOdd(A) {
    let count = {}
    let min = Infinity

    for (const num of A) {
        count[num] ? count[num]++ : count[num] = 1
    }

    for (const num in count) {
        if (count[num] % 2 !== 0 && min > parseInt(num)) {
            min = parseInt(num)
        }
    }

    return min === Infinity ? 0 : min
}

筆記

同時練習for...offor...in兩種用法

  1. for...of用於迭代陣列等可迭代物件的
  2. for...in用於迭代物件的屬性,包括可數的屬性。

#javascript #Codewars #for...in #for...of #object







Related Posts

什麼是 SPA ?

什麼是 SPA ?

TypeScript 函式定義字串型別相連和JavaScript String.prototype.concat() 的差異

TypeScript 函式定義字串型別相連和JavaScript String.prototype.concat() 的差異

Day06 LINE Login API

Day06 LINE Login API


Comments